package service.impl;

import java.util.List;

import dao.StoDao;
import dao.WareDao;
import dao.impl.StoDaoImpl;
import dao.impl.WareDaoImpl;
import entity.Sto;
import entity.Ware;
import service.WareService;

public class WareServiceImpl implements WareService {

	WareDao dao = new WareDaoImpl();
	StoDao stoDao = new StoDaoImpl();
	
	@Override
	public boolean save(Ware ware) {
		if(ware.getWareid() != null && ware.getWarename() != null &&
				ware.getProdarea() != null && ware.getSpecification() != null &&
				ware.getBatchnum() != null && ware.getSuppid()!= null ) {
			return dao.insert(ware);				
		}else {
			return false;
		}
	}

	// 删除，sto进货信息表中存在此商品,信息提示不能删除(因为添加外键没有on delete cascade)
	@Override
	public boolean remove(String wareid) {
		
		// (系统内调用，不需要对外公开此项业务则写在dao层，如果要对外公开服务就写在service层)
		// 先在sto表里查看wareid是否有
		List<Sto> stos = stoDao.selectByWareId(wareid);
		if (stos.isEmpty()) {
			return dao.delete(wareid);
		}else {
			System.out.println("不能删除，因为被sto表引用");
			return false;
		}
	}

	@Override
	public boolean modify(Ware ware) {
		if(ware.getWareid() != null && ware.getWarename() != null &&
				ware.getProdarea() != null && ware.getSpecification() != null &&
				ware.getBatchnum() != null && ware.getSuppid()!= null ) {
			return dao.update(ware);
		}else {
			return false;
		}
	}

	@Override
	public List<Ware> getByName(String warename) {
		return dao.selectByName(warename);
	}

	@Override
	public List<Ware> getBySuppname(String suppname) {
		return dao.selectBySuppname(suppname);
	}

}
